using BXS.API.Models;
using Microsoft.AspNetCore.Mvc;

namespace BXS.API.Controllers
{
    /// <summary>
    /// 商品管理控制器
    /// </summary>
    public class ProductsController : BaseController
    {
        // 模拟数据
        private static List<Product> _products = new List<Product>
        {
            new Product { Id = 1, Name = "玉石骨灰盒", Category = "骨灰容器", Price = 2999, OriginalPrice = 3999, Stock = 50, Status = "active", IsHot = true, IsRecommend = true, CreateTime = "2024-01-01", Image = "https://via.placeholder.com/80x80" },
            new Product { Id = 2, Name = "寿衣套装", Category = "寿衣", Price = 899, OriginalPrice = 1299, Stock = 100, Status = "active", IsHot = false, IsRecommend = true, CreateTime = "2024-01-02", Image = "https://via.placeholder.com/80x80" },
            new Product { Id = 3, Name = "白菊花束", Category = "鲜花花艺", Price = 299, OriginalPrice = 399, Stock = 200, Status = "active", IsHot = true, IsRecommend = false, CreateTime = "2024-01-03", Image = "https://via.placeholder.com/80x80" },
            new Product { Id = 4, Name = "纪念相框", Category = "纪念品", Price = 199, OriginalPrice = 299, Stock = 150, Status = "inactive", IsHot = false, IsRecommend = false, CreateTime = "2024-01-04", Image = "https://via.placeholder.com/80x80" },
        };

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="category">分类筛选</param>
        /// <returns>商品列表</returns>
        [HttpGet]
        public ActionResult<PagedApiResponse<Product>> GetProducts([FromQuery] int page = 1, [FromQuery] int pageSize = 10, [FromQuery] string? category = null)
        {
            var query = _products.AsQueryable();
            
            if (!string.IsNullOrEmpty(category) && category != "all")
            {
                query = query.Where(p => p.Category == category);
            }

            var total = query.Count();
            var skip = (page - 1) * pageSize;
            var products = query.Skip(skip).Take(pageSize).ToList();
            
            return PagedSuccess(products, total, page, pageSize);
        }

        /// <summary>
        /// 根据ID获取商品
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <returns>商品信息</returns>
        [HttpGet("{id}")]
        public ActionResult<ApiResponse<Product>> GetProduct(int id)
        {
            var product = _products.FirstOrDefault(p => p.Id == id);
            if (product == null)
            {
                return Error<Product>("商品不存在", 404);
            }
            return Success(product);
        }

        /// <summary>
        /// 创建商品
        /// </summary>
        /// <param name="request">创建商品请求</param>
        /// <returns>创建结果</returns>
        [HttpPost]
        public ActionResult<ApiResponse<Product>> CreateProduct([FromBody] CreateProductRequest request)
        {
            if (string.IsNullOrEmpty(request.Name) || string.IsNullOrEmpty(request.Category) || request.Price <= 0 || request.Stock < 0)
            {
                return Error<Product>("请填写完整信息");
            }

            var newProduct = new Product
            {
                Id = _products.Count > 0 ? _products.Max(p => p.Id) + 1 : 1,
                Name = request.Name,
                Category = request.Category,
                Price = request.Price,
                OriginalPrice = request.OriginalPrice,
                Stock = request.Stock,
                Status = request.Status,
                IsHot = request.IsHot,
                IsRecommend = request.IsRecommend,
                CreateTime = DateTime.Now.ToString("yyyy-MM-dd"),
                Image = request.Image
            };

            _products.Add(newProduct);
            return Success(newProduct, "商品创建成功");
        }

        /// <summary>
        /// 更新商品
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <param name="request">更新商品请求</param>
        /// <returns>更新结果</returns>
        [HttpPut("{id}")]
        public ActionResult<ApiResponse<Product>> UpdateProduct(int id, [FromBody] UpdateProductRequest request)
        {
            var product = _products.FirstOrDefault(p => p.Id == id);
            if (product == null)
            {
                return Error<Product>("商品不存在", 404);
            }

            if (string.IsNullOrEmpty(request.Name) || string.IsNullOrEmpty(request.Category) || request.Price <= 0 || request.Stock < 0)
            {
                return Error<Product>("请填写完整信息");
            }

            product.Name = request.Name;
            product.Category = request.Category;
            product.Price = request.Price;
            product.OriginalPrice = request.OriginalPrice;
            product.Stock = request.Stock;
            product.Status = request.Status;
            product.IsHot = request.IsHot;
            product.IsRecommend = request.IsRecommend;
            product.Image = request.Image;

            return Success(product, "商品更新成功");
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public ActionResult<ApiResponse<bool>> DeleteProduct(int id)
        {
            var product = _products.FirstOrDefault(p => p.Id == id);
            if (product == null)
            {
                return Error<bool>("商品不存在", 404);
            }

            _products.Remove(product);
            return Success(true, "商品删除成功");
        }

        /// <summary>
        /// 获取商品分类列表
        /// </summary>
        /// <returns>分类列表</returns>
        [HttpGet("categories")]
        public ActionResult<ApiResponse<List<string>>> GetCategories()
        {
            var categories = _products.Select(p => p.Category).Distinct().ToList();
            return Success(categories);
        }
    }
} 